package com.ll.base.dto;

import com.ll.base.enums.ResultCodeEnum;
import com.ll.base.exception.CustomRunTimeException;

import java.io.Serializable;

/**
 *Api层返回对象列表封装
 * @param <T>
 */
public class ApiResultDTO<T>  implements Serializable{

    private boolean success = false;

    private String code;

    private String message;

    private T result;

    public ApiResultDTO() {
    }

    public static <T> ApiResultDTO<T> success() {
       return success(null);
    }

    public static <T> ApiResultDTO<T> success(T result) {
        ApiResultDTO<T> item = new ApiResultDTO<T>();
        item.success = true;
        item.result = result;
        item.code = "0";
        item.message = "success";
        return item;
    }


    public static <T> ApiResultDTO<T> failure(CustomRunTimeException codeEnum) {
        ApiResultDTO<T> item = new ApiResultDTO<T>();
        item.success = false;
        item.code = codeEnum.getErrorCode();
        item.message = codeEnum.getErrorMsg();
        return item;
    }

    public static <T> ApiResultDTO<T> failure(ResultCodeEnum codeEnum) {
        ApiResultDTO<T> item = new ApiResultDTO<T>();
        item.success = false;
        item.code = codeEnum.getCode();
        item.message = codeEnum.getMsg();
        return item;
    }

    public static <T> ApiResultDTO<T> failure(String errorCode, String errorMessage) {
        ApiResultDTO<T> item = new ApiResultDTO<T>();
        item.success = false;
        item.code = errorCode;
        item.message = errorMessage;
        return item;
    }

    public static <T> ApiResultDTO<T> failure(String errorCode) {
        ApiResultDTO<T> item = new ApiResultDTO<T>();
        item.success = false;
        item.code = errorCode;
        item.message = "failure";
        return item;
    }

    public boolean hasResult() {
        return result != null;
    }

    public boolean isSuccess() {
        return success;
    }

    public T getResult() {
        return result;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}
